package cn.xspace.module.other.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.ClassUtil;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.Navigation;
import com.xspace.android.xafcommon.util.PreferencesUtils;
import com.xspace.android.xafcommon.util.WorkLogConstant;

import java.util.ArrayList;
import java.util.List;

import cn.xspace.library.util.Tools;
import cn.xspace.module.mywork.activity.OfflineDataActivity;
import cn.xspace.module.other.R;
import cn.xspace.module.other.activity.AboutActivity;
import cn.xspace.module.other.activity.AlarmActivity;
import cn.xspace.module.other.activity.CacheCleanActivity;
import cn.xspace.module.other.activity.MyFavoriteActivity;
import cn.xspace.module.other.activity.RecentLoginActivity;
import cn.xspace.module.other.activity.SubscriptionManageActivity;
import cn.xspace.module.other.activity.share.WnxWebViewAct;
import cn.xspace.module.other.application.MyApplication;
import cn.xspace.module.other.bean.SettingBean;
import cn.xspace.module.other.bean.SettingBean.SettingEnum;
import cn.xspace.module.other.util.RProxy;

import static cn.xspace.module.other.bean.SettingBean.SettingEnum.Alarm;
import static cn.xspace.module.other.bean.SettingBean.SettingEnum.MyFavouritePage;

public class SettingAdapter extends BaseAdapter {

    public List<SettingBean> mList = new ArrayList<SettingBean>();
    public Context mContext;

    public SettingAdapter(Context mContext, List<SettingBean> list) {
        this.mContext = mContext;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return this.mList.size();
    }

    @Override
    public Object getItem(int index) {
        return this.mList.get(index);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.place_set_item, null);
        }
//        if (position == 0) {
//            convertView.setBackgroundResource(R.drawable.preference_first_item);
//        } else if (position == mList.size() - 1) {
//            convertView.setBackgroundResource(R.drawable.preference_last_item);
//        } else {
//            convertView.setBackgroundResource(R.drawable.preference_item);
//        }
        ImageView img = (ImageView) convertView.findViewById(R.id.img1);
        ImageView img2 = (ImageView) convertView.findViewById(R.id.img2);
        TextView tv_line = (TextView) convertView.findViewById(R.id.setting_item_line);
        TextView tv_upline = (TextView) convertView.findViewById(R.id.setting_item_upline);
        if (position == mList.size() - 1) tv_line.setVisibility(View.GONE);
        if (position == 1) tv_upline.setVisibility(View.VISIBLE);
        if (mList.get(position).key == SettingEnum.CheckVersion) { //在版本更新的位置上加上发现新版本的标签
            Log.e("wubaojie--", "版本更新的位置");
            if (PreferencesUtils.getBooleanData("has_new_version")) {
                img2.setImageResource(R.drawable.center_update_new);
            }
        } else {
            img2.setImageResource(R.drawable.arrow_right_new);
        }
        img.setVisibility(View.GONE);
        TextView tv_set = (TextView) convertView.findViewById(RProxy.tv_place);
        tv_set.setTextSize(16);
        tv_set.setText(mList.get(position).title);
        viewOnClick(convertView, mList.get(position).key);
        return convertView;
    }

    /**
     * @param @param rowView
     * @return
     * @throws
     * @Description: TODO
     */
    private void viewOnClick(View rowView, final SettingEnum key) {

        rowView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (key == SettingEnum.SubscribeManager || key == MyFavouritePage
                        || key == Alarm || key == SettingEnum.AccountSetting || key == SettingEnum.Worklog || key ==
                        SettingEnum.FeedBack) {
                    if (!MyApplication.get().isAccountConfigured()) {
                        Navigation.loginConfirm(mContext);
                        return;
                    }
                }
                switch (key) {
//                    case AccountSetting:
//                        mContext.startActivity(new Intent(mContext, UserInfoAct.class));
//                        break;
                    case SubscribeManager:
                        mContext.startActivity(new Intent(mContext, SubscriptionManageActivity.class));
                        break;
                    case Worklog:
                        Class clz = ClassUtil.getClassByName(ClassUtil.myWorkActivity);
                        if (clz != null) {
                            mContext.startActivity(new Intent(mContext, clz));
                        }
                        break;
                    case MyFavouritePage:
                        mContext.startActivity(new Intent(mContext, MyFavoriteActivity.class));
                        break;
                    case Alarm:
                        mContext.startActivity(new Intent(mContext, AlarmActivity.class));
                        break;
                    case CleanCache:
                        mContext.startActivity(new Intent(mContext, CacheCleanActivity.class));
                        break;
                    case CheckVersion:
                        Tools.detectionUpdateReq(mContext, true);
                        break;
                    case About:
                        mContext.startActivity(new Intent(mContext, AboutActivity.class));
                        break;
                    case HeloDoc:
//                        new MAFJsPlugin(mContext).toActivity("native", "native", "browser", WorkLogConstant.HELP_DOC);
//                        Intent intent = new Intent(mContext, WnxWebViewAct.class);
//                        intent.putExtra(Constants.TITITL, "帮助文档");
//                        intent.putExtra(Constants.WEBVIEURL, WorkLogConstant.HELP_DOC);
//                        intent.putExtra("showMore", false);
//                        mContext.startActivity(intent);
//                        Map<String, Object> dataMap = new HashMap<String, Object>();
//                        dataMap.put(Constants.WEBVIEURL, WorkLogConstant.HELP_DOC);
//                        SkipTools.initModuleApk(mContext, "ExWebAct", dataMap);
                        Intent intent = new Intent(mContext, WnxWebViewAct.class);
                        intent.putExtra(Constants.TITITL, "帮助文档");
                        intent.putExtra(Constants.WEBVIEURL, WorkLogConstant.HELP_DOC);
                        intent.putExtra("showMore", false);
                        mContext.startActivity(intent);
                        break;
//                    case FeedBack:
//                        mContext.startActivity(new Intent(mContext, UserFeedbackAct.class));
//                        break;
                    case DeviceMgmt:
                        mContext.startActivity(new Intent(mContext, RecentLoginActivity.class));
                        break;
                    case OfflineData:
                        mContext.startActivity(new Intent(mContext, OfflineDataActivity.class));
                    default:
                        break;
                }
            }

        });
    }


}
